<template>
  <el-container>
    <!-- 页面顶部区域 -->
    <el-header>
      <!-- logo 标题 区域 -->
      <div class="login">
        <img src="@/assets/img/logos.png" alt="" />
        <span>杭州23期后台管理系统</span>
      </div>
      <!-- 退出 -->
      <el-button type="warning" icon="el-icon-switch-button" @click="signout"
        >退出</el-button
      >
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧菜单栏 -->
      <el-aside :width="isfolding ? '64px' : '220px'">
        <div class="zhedie" @click="folding">|||</div>
        <el-menu
          unique-opened
          router
          :default-active="activePath"
          :collapse="isfolding"
          :collapse-transition="false"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#1E9FFF"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + ele.path"
              v-for="ele in item.children"
              :key="ele.id"
              @click="savePath('/' + ele.path)"
            >
              <template slot="title">
                <i class="iconfont icon-weidingyicaidan"></i>
                <span>{{ ele.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { menusList } from '@/api/home.js'
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      isfolding: false,
      menulist: [],
      activePath: '',
      iconObj: {
        125: 'iconfont icon-User',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-huiyuanguanli',
        145: 'iconfont icon-jituanjiance'
      }
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    ...mapActions(['user/logout']),
    // 获取菜单列表
    async getMenuList () {
      const data = await menusList()
      this.menulist = data
    },
    // 退出
    signout () {
      this['user/logout']()
      this.$router.push('./login')
    },
    // 折叠菜单
    folding () {
      this.isfolding = !this.isfolding
    },
    // 获取菜单分类的二级路由
    savePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
  overflow: hidden;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #373d41;
  }
  .el-aside {
    background: #333744;
    .el-menu {
      border-right: none;
    }
  }
  .zhedie {
    background: #666666;
    font-size: 10px;
    line-height: 40px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.3em;
    cursor: pointer;
    user-select: none; /**双击禁止选中 */
  }
  .el-main {
    background: #eaedf1;
  }
  .iconfont {
    margin-right: 9px;
    font-size: 18px;
    color: #fff;
  }
}
.login {
  display: flex;
  align-items: center;
  span {
    font-size: 25px;
    color: #fff;
    margin-left: 10px;
  }
}
</style>
